package cn.tedu.boot101.controller;

import cn.tedu.boot101.mapper.CommentMapper;
import cn.tedu.boot101.mapper.WeiboMapper;
import cn.tedu.boot101.pojo.dto.CommentDto;
import cn.tedu.boot101.pojo.entity.Comment;
import cn.tedu.boot101.pojo.entity.Weibo;
import cn.tedu.boot101.pojo.vo.CommentVO;
import cn.tedu.boot101.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentMapper mapper;

    @RequestMapping("/insert")
    public int insert(@RequestBody CommentDto commentDto, HttpSession session) {
        UserVO u = (UserVO) session.getAttribute("user");
        if (u == null) {
            return 2;
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDto, comment);
        comment.setUser_id(u.getId());
        comment.setCreated(new Date());
        mapper.insert(comment);
        return 1;
    }

    @RequestMapping("/select")
    public List<CommentVO> select(int id) {
        System.out.println(id);
        return mapper.selectByWeiboId(id);
    }
}
